#include "StdAfx.h"
#include ".\evaluatingfunction.h"


CEvaluatingFunction::CEvaluatingFunction(double Rmin, double Rmax, size_t FunChoice, size_t  FCN):
RangeMin(Rmin), RangeMax(Rmax), FitnessCasesNumber(FCN){
	if(RangeMin >= RangeMax) throw CString(_T("Invalid range at CEvaluatingFunction construction\r\n"));
	this->chooseFunction(FunChoice);
	this->Function = new BoundedFunctionDescription(funcRep, RangeMin.x(), RangeMax.x(), FitnessCasesNumber);

}

CEvaluatingFunction::~CEvaluatingFunction(void){
	delete this->Function;
}

F<double> Eval0(F<double> Xval){
	return (F<double>)-1.0 + (F<double>)(2.0)* Xval*Xval + (F<double>)(0.5)*Xval*Xval*Xval;
	}
F<double> Eval1(F<double> Xval){
	return (F<double>)-4.0 + (F<double>)(2.0)* Xval + (F<double>)(3.0)*Xval*Xval;
	}

F<double> Eval2(F<double> Xval){
	return  (F<double>)1.0 + (F<double>)(2.0)* Xval*Xval + (F<double>)(3.0)*Xval*Xval*Xval*Xval;
	}

F<double> Eval3(F<double> Xval){
	return (F<double>)1.0 + (F<double>)(2.0)*Xval + (F<double>)(3.0)*Xval*Xval*Xval*Xval;
	}

F<double> Eval4(F<double> Xval){
	return  sin(Xval);
	}

F<double> Eval5(F<double> Xval){
	return  (cos(Xval)*(F<double>)(7.0))*sin(Xval);// + (F<double>)(3.0)*Xval*Xval;
	}

F<double> Eval6(F<double> Xval){
	return (F<double>)1.0 - (F<double>)(2.0)*Xval - (F<double>)(1.5)*Xval*Xval*Xval;
	}

F<double> Eval7(F<double> Xval){
	return (F<double>)1.0 - (F<double>)(2.0)*Xval - (F<double>)(1.5)*Xval*Xval*Xval*Xval;
	}

F<double> Eval8(F<double> Xval){
	return (F<double>)-2.0 + (F<double>)(2.0)* Xval  + (F<double>)(1.5)*Xval*Xval - (F<double>)(3.0)*Xval*Xval*Xval;
	}
F<double> Eval9(F<double> Xval){
	return Xval;
	}

pair<BoundedFunctionDescription::Evaluate, string> CEvaluatingFunction::getChoices(size_t num){

	BoundedFunctionDescription::Evaluate Funcs[ ]= {
			&Eval0,
			&Eval1,
			&Eval2,
			&Eval3,
			&Eval4,
			&Eval5,
			&Eval6,
			&Eval7,
			&Eval8,
			&Eval9
		};
	string FuncStringReps[ ] = {
		string("-1 + 2(x^2) + 0.5(x^3)"),
		string("-4 + (2)* Xval + (3)*Xval*Xval"),
		string("1 + (2)* Xval*Xval + (3)*Xval*Xval*Xval*Xval"),
		string("1 + (2)*Xval + (3)*Xval*Xval*Xval*Xval"),
		string("sin(x)"),
		string("7 * cos(x) * sin(x)"),
		string("1 - (2)*Xval - (1.5)*Xval*Xval*Xval"),
		string("1 - (2)*Xval - (1.5)*Xval*Xval*Xval*Xval"),
		string("-2 + (2)* Xval  + (1.5)*Xval*Xval - (3)*Xval*Xval*Xval"),
		string("Xval")
		};

		return pair<BoundedFunctionDescription::Evaluate, string> (Funcs[num], FuncStringReps[num]);
	}


void CEvaluatingFunction::chooseFunction(size_t FunChoice){
	pair<BoundedFunctionDescription::Evaluate, string> PES = CEvaluatingFunction::getChoices(FunChoice);
	funChoice = FunChoice;
	funcRep = BoundedFunctionDescription::FuncRep(PES.first, PES.second);
}
void CEvaluatingFunction::draw(size_t Parames){

	Function->draw();
	if(Parames == DRAW_GRID){
		glColor3f(0.0f, 1.0f, 0.0f);
		glBegin(GL_LINES);
				glVertex3f(0.0f, 10.0f, 1.0f);
				glVertex3f(0.0f, -10.0f, 1.0f);
				glVertex3f(-10.0f, 0.0f, 1.0f);
				glVertex3f(10.0f, 0.0f, 1.0f);
		glEnd();
		}

}










